package org.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.example.service.HystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@DefaultProperties(defaultFallback = "global_fallback")
public class HystrixController {

    @Resource
    private HystrixService hystrixService;

    @GetMapping("/consumer/hystrix/ok")
    public String test_ok(){
        String info = hystrixService.test_ok();
        return info;
    }

    //消费者客户端方设置熔断机制
    @GetMapping("/consumer/hystrix/timeout")
/*    @HystrixCommand(fallbackMethod = "timeout_fallback",commandProperties ={
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="2000")
    } )*/
    @HystrixCommand
    public String test_timeOut(){
//        int i  = 1/0;
        return hystrixService.test_timeOut();
    }

    public String timeout_fallback(){
        return "系统繁忙，请稍后重试!!!!!";
    }

    //@HystrixCommand是局部的fallback配置,若指定降级兜底方案则使用，若没有指定，则使用全局fallback
    //下面定义全局的fallback方法
    public String global_fallback(){
        return "全局系统繁忙，请稍后重试!!!!!";
    }

}
